<template>
  <view class="btn-group" ref="dropdown">
    <view class="main_title flex">
      <view class="main_nav flex">
        <view class="btn-nav" :class="{ 'active' : menuActiveList[0] }" @click="toggleMenu('style')">
          <view class ="nav-title">风格</view>
          <image class="main_icon" src="/static/img/sj.png"></image>
        </view>
      </view>
      <view class="main_nav flex">
        <view class="btn-nav" :class="{ 'active' : menuActiveList[1] }" @click="toggleMenu('place')">
          <view class ="nav-title">产地</view>
          <image class="main_icon" src="/static/img/sj.png"></image>
        </view>
      </view>
      <view class="main_nav flex">
        <view class="btn-nav" :class="{ 'active' : menuActiveList[2] }" @click="toggleMenu('vol')">
          <view class ="nav-title">酒精度</view>
          <image class="main_icon" src="/static/img/sj.png"></image>
        </view>
      </view>
      <view class="main_nav flex">
        <view class="btn-nav" :class="{ 'active' : menuActiveList[3] }" @click="toggleMenu('price')">
          <view class ="nav-title">价格</view>
          <image class="main_icon" src="/static/img/sj.png"></image>
        </view>
      </view>
    </view>

    <ul class="dropdown-menu" :class="{ 'show' : showMenuList[0] }">
      <li v-for="(option, idx) in styleList" :key="idx">
        <a
          href="javascript:void(0)"
          :class="{'active': selectedStyle && selectedStyle.name === option.name}"
          @click="updateOption(option, 0)"
        >
          {{ option.name }}
        </a>
      </li>
    </ul>
    <ul class="dropdown-menu" :class="{ 'show' : showMenuList[1] }">
      <li v-for="(option, idx) in placeList" :key="idx">
        <a
          href="javascript:void(0)"
          :class="{'active': selectedPlace && selectedPlace.name === option.name}"
          @click="updateOption(option, 1)"
        >
          {{ option.name }}
        </a>
      </li>
    </ul>
    <ul class="dropdown-menu" :class="{ 'show' : showMenuList[2] }">
      <li v-for="(option, idx) in volList" :key="idx">
        <a
          href="javascript:void(0)"
          :class="{'active': selectedVol && selectedVol.name === option.name}"
          @click="updateOption(option, 2)"
        >
          {{ option.name }}
        </a>
      </li>
    </ul>
    <ul class="dropdown-menu" :class="{ 'show' : showMenuList[3] }">
      <li v-for="(option, idx) in priceList" :key="idx">
        <a
          href="javascript:void(0)"
          :class="{'active': selectedPrice && selectedPrice.name === option.name}"
          @click="updateOption(option, 3)"
        >
          {{ option.name }}
        </a>
      </li>
    </ul>
  </view>
</template>

<script>
import { GOODS_STYLE_LIST, GOODS_PLACE_LIST, GOODS_VOL_PRICE_LIST } from '@/api';
export default {
  data() {
    return {
      selectedStyle: {},
      selectedPlace: {},
      selectedVol: {},
      selectedPrice: {},
      showMenuList: [0,0,0,0],
      menuActiveList: [0,0,0,0],
      styleList: [{name: '全部', id: -1}],
      placeList: [{name: '全部', id: -1}],
      volList: [{name: '全部', id: -1}],
      priceList: [{name: '全部', id: -1}],
      gType: null
    };
  },
  created() {
    this.getStyleList();
    this.getPlaceList();
    this.getVolAndPriceList()
  },
  // mounted() {
  //   this.$nextTick(() => {
  //     document.addEventListener("click", e => {
  //       let el = document.querySelector("#drop" + that.gType);
  //       if (el && !el.contains(e.target)) {
  //         //点击除弹出层外的空白区域
  //         that.showMenuList = [0,0,0,0];
  //       }
  //     });
  //   });
  // },
  methods: {
    // 商品风格列表
    getStyleList() {
      let that = this
      that.$_get(GOODS_STYLE_LIST).then(res => {
        if (res.code === 1) {
          that.styleList = that.styleList.concat(res.data.map(item => {
            let _item = item
            _item.name = item.style_name
            return _item
          }))
        }
      });
    },
    // 商品产地列表
    getPlaceList() {
      let that = this
      that.$_get(GOODS_PLACE_LIST).then(res => {
        if(res.code === 1){
          that.placeList = that.placeList.concat(res.data.map(item => {
            let _item = item
            _item.name = item.place
            return _item
          }))
        }
      });
    },
    // 获取酒精度&价格
    getVolAndPriceList () {
      let that = this
      that.$_get(GOODS_VOL_PRICE_LIST).then(res => {
        if(res.code === 1){
          that.volList = that.volList.concat(res.data.vol)
          that.priceList = that.priceList.concat(res.data.price)
        }
      });
    },
    updateOption: function(option, idx) {
      this.showMenu = false;
      option.type = this.gType;
      if (this.gType === "style") {
        this.selectedStyle = option;
      } else if (this.gType === "place") {
        this.selectedPlace = option;
      } else if (this.gType === "vol") {
        this.selectedVol = option;
      } else if (this.gType === "price") {
        this.selectedPrice = option;
      }
      if (option.id < 0) {
        this.$set(this.menuActiveList, idx, 0)
      } else {
        this.$set(this.menuActiveList, idx, 1)
      }
      this.showMenuList = [0,0,0,0]
      this.$emit("updateOption", option);
    },
    toggleMenu (type) {
      this.gType = type
      let typleList = ['style', 'place', 'vol', 'price']
      let idx = typleList.indexOf(type)
      let showMenu = this.showMenuList[idx]
      if (showMenu) {
        this.showMenuList = [0,0,0,0]
      } else {
        this.showMenuList = [0,0,0,0]
        this.$set(this.showMenuList, idx, 1)
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.btn-group {
  min-width: 100px;
  display: inline-block;
  vertical-align: middle;
  a {
    &:hover {
      text-decoration: none;
    }
  }
}
.dropdown-toggle {
  list-style-type: none;
  color: #ffffff;
  min-width: 100px;
  padding: 10px;
  text-transform: none;
  font-weight: 300;
  margin-bottom: 7px;
  border: 0;
  border-radius: 0;
}

.dropdown-menu {
  position: fixed;
  top: 13vw;
  left: 9vw;
  z-index: 1000;
  width: 66vw;
  padding: 5px;
  box-sizing: border-box;
  margin: 2px 0 0;
  list-style: none;
  font-size: 14px;
  text-align: left;
  background-color: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  display: none;
  &.show {
    display: block;
  }
}
.dropdown-menu > li > a {
  padding: 5px 1.5vw;
  display: block;
  clear: both;
  font-weight: normal;
  line-height: 1.6;
  color: #333333;
  white-space: nowrap;
  text-decoration: none;
  border-radius: 5px;
  &:hover {
    background: #efefef;
    color: #409fcb;
  }
  &.active {
    background-color: #c8ad67;
    color: #fff;
  }
}
.dropdown-menu > li {
  overflow: hidden;
  display: inline-block;
  margin: 0;
  width: 25%;
  text-align: center;
}
.btn-nav {
  line-height: 48px;
  &.active{
    color: #c8ad67;
  }
  .nav-title {
    display: inline-block;
    font-size: 15px;
  }
  .main_icon {
    width: 12px;
    height: 8px;
    margin-left: 14px;
  }
}
.main_title {
	background: #2a2a2a;
  padding: 0 5vw;
  margin-bottom: 14px;
	width: 72vw;
	position: fixed;
	top:8vw;
	left: 0;
  z-index: 1;
  height: 5vw;
}
.main_nav {
  flex: 1;
  justify-content: center;
  color: #ffffff;
  font-size: 20px;
  font-weight: bold;
  width: 160px;
  align-items: center;
}
.main_icon {
  width: 12px;
  height: 8px;
  margin-left: 14px;
}
</style>

